package com.example.wsServer;

import com.example.netty.UserAuthHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.handler.timeout.IdleStateHandler;
import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;
import java.util.concurrent.ScheduledExecutorService;

/**
 * Author by admin, Email xx@xx.com, Date on 2020/5/19.
 * PS: Not easy to write code, please indicate.
 */
@Slf4j
public class wsserver {
    private ScheduledExecutorService executorService;
     private  static  final wsserver ws=new wsserver();
     private ServerBootstrap serverBootstrap;
     private NioEventLoopGroup boos;
     private   NioEventLoopGroup worker;
     private  ChannelFuture future;
    public static wsserver getInstance(){
        return ws;
     }
    private wsserver(){
          serverBootstrap = new ServerBootstrap();
         //主线程组  负责拉客
          boos = new NioEventLoopGroup();
         //从线程组  接待客人，安排入座
          worker = new NioEventLoopGroup();
           serverBootstrap
                     .group(boos, worker)//设置主从
                     .channel(NioServerSocketChannel.class)//设置nio双向通道
                     .childOption(ChannelOption.SO_KEEPALIVE, true)
                     .childOption(ChannelOption.TCP_NODELAY, true)
                     .childHandler(new ChannelInitializer<SocketChannel>() //处理任务
                     {
                         @Override
                         protected void initChannel(SocketChannel ch) {
                             ch.pipeline().addLast(new HttpServerCodec());//解析http请求解码器
                             ch.pipeline().addLast(new ChunkedWriteHandler());//处理大数据，分块写入   //支持异步发送大的码流，一般用于发送文件流
                             ch.pipeline().addLast(new HttpObjectAggregator(1024 * 64));//将多个消息转换成单一的消息对象
                             //==========      以上是支持http协议    ==========//
//                             ch.pipeline().addLast(new WebSocketServerProtocolHandler("/ws"));//指点客户端访问的路由
                             //检测链路是否读空闲
                             ch.pipeline().addLast(new IdleStateHandler(60, 0, 0));
                             ch.pipeline().addLast(new UserAuthHandler());
                             ch.pipeline().addLast(new MessageHandler());

//                             ch.pipeline().addLast(new MyWebSocketHandler());//处理消息的发送
                         }
                     });
         };
     public void start(int port){
         try {
             //启动服务设置端口，开启同步
             future  = serverBootstrap.bind(port).sync();
             InetSocketAddress addr = (InetSocketAddress) future.channel().localAddress();
             log.info("WebSocketServer start success, port is:{}", addr.getPort());
         } catch (InterruptedException e) {
             log.error("WebSocketServer start fail,", e);
         }
     }
}
